package com.dx.mapper.impl;

import com.dx.domain.SysUser;
import com.dx.mapper.SysUserMapper;
import com.dx.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SysUserMapperImpl
 * @Description
 * @Author 12994
 * @Date 2024/10/17 13:23
 * @Version 1.0
 **/
public class SysUserMapperImpl implements SysUserMapper {
    SqlSession sqlSession;

    @Override
    public ArrayList<SysUser> listSysUsers() {
        // 获取Session
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).listSysUsers();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public SysUser findUserByUserId(Long userId) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).findUserByUserId(userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public SysUser findUserByUserIdAndUserName(Long userId, String userName) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).findUserByUserIdAndUserName(userId, userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int addSysUser(SysUser sysUser) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).addSysUser(sysUser);
            /*
            当ssf.openSession(false)时, 需要手动提交/回滚事务:
            int count = sqlSession.getMapper(SysUserMapper.class).addSysUser(sysUser);
            sqlSession.commit();
            return count;
             */
        } catch (Exception e) {
            /*
            当发生异常时, 回滚
            sqlSession.rollback();
             */
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int updateSysUser(SysUser sysUser) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).updateSysUser(sysUser);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public int removeSysUserByUserId(Long userId) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).removeSysUserByUserId(userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public ArrayList<SysUser> listSysUsersLike(String userName) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).listSysUsersLike(userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    @Override
    public ArrayList<SysUser> findUserBySysUser(SysUser sysUser) {
        try {
            sqlSession = MybatisUtil.getSqlSession();
            return sqlSession.getMapper(SysUserMapper.class).findUserBySysUser(sysUser);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            MybatisUtil.releaseSqlSession(sqlSession);
        }
    }

    public static void main(String[] args) {
        SysUserMapper sysUserMapper = new SysUserMapperImpl();
        System.out.println(sysUserMapper.findUserByUserId(Long.parseLong("1")));
        System.out.println("-------------------------------");

        SysUser newUser = new SysUser(null,
                "张三", "123456",
                "13111111111", "12345@gmail.com",
                "110110190001011010", "中国新疆Urumqi",
                new Date(), Long.parseLong("1000"));
        System.out.println(sysUserMapper.addSysUser(newUser));
        sysUserMapper.listSysUsers().forEach(System.out::println);
        System.out.println("-------------------------------");

        newUser.setSysUserId(Long.parseLong("12"));
        newUser.setSysUserName("王紫阳");
        System.out.println(sysUserMapper.updateSysUser(newUser));
        sysUserMapper.listSysUsers().forEach(System.out::println);
        System.out.println("-------------------------------");

        System.out.println(sysUserMapper.removeSysUserByUserId(13L));
        sysUserMapper.listSysUsers().forEach(System.out::println);
        System.out.println("-------------------------------");

        sysUserMapper.listSysUsersLike("张").forEach(System.out::println);
        System.out.println("----------姓名模糊查询结束-----------------");

        newUser.setSysUserName("王");
        newUser.setSysUserPassword("123456");
        System.out.println(sysUserMapper.findUserBySysUser(newUser));
        System.out.println("---------根据用户名和密码模糊查询结束-----------");
    }
}
